package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BusCustomer;
import cn.wolfcode.car.business.mapper.BusCustomerMapper;
import cn.wolfcode.car.business.query.BusCustomerQuery;
import cn.wolfcode.car.business.service.IBusCustomerService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Transactional
public class BusCustomerServiceImpl implements IBusCustomerService {

    @Autowired
    private BusCustomerMapper busCustomerMapper;


    @Override
    public TablePageInfo<BusCustomer> query(BusCustomerQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<BusCustomer>(busCustomerMapper.selectForList(qo));
    }


    @Override
    public void save(BusCustomer busCustomer) {
        if (busCustomerMapper.selectByPhone(busCustomer.getPhone()) != null){
            throw new BusinessException("已存在该手机号码的用户");
        }
        busCustomerMapper.insert(busCustomer);
    }

    @Override
    public BusCustomer get(Long id) {
        return busCustomerMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(BusCustomer busCustomer) {
        busCustomerMapper.updateByPrimaryKey(busCustomer);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            busCustomerMapper.sab_delete(dictId);
        }
    }

    @Override
    public List<BusCustomer> list() {
        return busCustomerMapper.selectAll();
    }

    @Override
    public Workbook export() {
        List<BusCustomer> busCustomers = busCustomerMapper.selectAll();
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("客户列表");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("联系方式");
        row.createCell(3).setCellValue("生日");
        row.createCell(4).setCellValue("是否vip");
        row.createCell(5).setCellValue("备注");
        BusCustomer busCustomer = null;
        for(int i=0;i<busCustomers.size();i++){
            row = sheet.createRow(i+1);
            busCustomer = busCustomers.get(i);
            row.createCell(0).setCellValue(1 + i);
            row.createCell(1).setCellValue(busCustomer.getName());
            row.createCell(2).setCellValue(busCustomer.getPhone());
            String birthday = new SimpleDateFormat("yyyy-MM-dd").format(busCustomer.getBirthday());
            row.createCell(3).setCellValue(birthday);
            String vip = busCustomer.getVip() == 0 ? "否" : "是";
            row.createCell(4).setCellValue(vip);
            row.createCell(5).setCellValue(busCustomer.getRemark());
        }
        return workbook;
    }
}
